import request from "../../utils/request";
import pubsub from 'pubsub-js'
// pages/recommend/recommend.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        month: '',
        day: '',
        recommendList: [],
        index: 0
    },

    toSongDetail(e) {
        const {
            song,
            index
        } = e.currentTarget.dataset;

        this.setData({
            index
        })
        wx.navigateTo({
            // url: '/pages/songDetail/songDetail?song=' + JSON.stringify(song),
            url: '/pages/songDetail/songDetail?musicId=' + song.id,
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        pubsub.subscribe('switchType', (msg, typeObj) => {
            let {
                recommendList,
                index
            } = this.data;

            let {
                type,
                modeId
            } = typeObj;
            // console.log(modeId);

            // 随机播放产生的index
           
            if (modeId === 1) {
                index = Math.floor(Math.random() * recommendList.length);
            }

            function getNewSongIndex(type, index) {
                let curIndex = index;
                if (type === 'prev') {
                    (curIndex === 0) && (curIndex = recommendList.length);
                    curIndex--;
                } else {
                    (curIndex === recommendList.length - 1) && (curIndex = -1);
                    curIndex++;
                }
                return curIndex;
            }

            this.setData({
                index: getNewSongIndex(type, index)
            })

            const musicId = recommendList[index].id;
            pubsub.publish('musicId', musicId);
        })

        let userInfo = wx.getStorageSync('userInfo');

        if (!userInfo) {
            wx.showToast({
                title: '请先登录',
                icon: 'none',
                success: () => {
                    wx.reLaunch({
                        url: '/pages/login/login',
                    })
                }
            })
        }
        this.setData({
            month: new Date().getMonth() + 1,
            day: new Date().getDate()
        })

        this.getRecommendList();
    },

    async getRecommendList() {
        let {
            data: recommendListData
        } = await request('/recommend/songs');
        // console.log(recommendListData.recommend);
        this.setData({
            recommendList: recommendListData.recommend
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})